//
//  TeacherDetailsViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/3/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "TeacherDetailsViewController.h"
#import "NSString+Empty.h"
#import "UIViewAdditions.h"
#import "WebBrowserViewController.h"
#import "Constants.h"


@interface TeacherDetailsViewController ()

@end

#define VERTICAL_SPACE (IS_IPAD () ? 40 : 15)

@implementation TeacherDetailsViewController

@synthesize teacherWebsiteButton, emailButton, phoneButton;

- (id)initWithNibName:(NSString *)nibNameOrNil teacher:(Teacher *)teacher
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        // Custom initialization
        _teacher = teacher;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Set up the UI
    CGFloat offsetY = teacherWebsiteButton.top;
    if (![NSString isNullOrEmpty:_teacher.teacherUrl]) {
        teacherWebsiteButton.hidden = NO;
        [Utils customizeButton:teacherWebsiteButton withBgImageKey:@"button_image" imageKey:nil textKey:@"TEACHER_WEBSITE_BUTTON" textColorKey:@"welcome_screen_buttons_text_color"];
        offsetY = teacherWebsiteButton.bottom + VERTICAL_SPACE;
    }
    if (![NSString isNullOrEmpty:_teacher.email]) {
        emailButton.hidden = NO;
        emailButton.top = offsetY;
        [Utils customizeButton:emailButton withBgImageKey:@"button_image" imageKey:nil textKey:nil textColorKey:@"welcome_screen_buttons_text_color"];
        offsetY = emailButton.bottom + VERTICAL_SPACE;
        [emailButton setTitle:_teacher.email forState:UIControlStateNormal];
    }
    if (![NSString isNullOrEmpty:_teacher.phone]) {
        phoneButton.hidden = NO;
        phoneButton.top = offsetY;
        [Utils customizeButton:phoneButton withBgImageKey:@"button_image" imageKey:nil textKey:nil textColorKey:@"welcome_screen_buttons_text_color"];
        offsetY += phoneButton.bottom + VERTICAL_SPACE;
        [phoneButton setTitle:_teacher.phone forState:UIControlStateNormal];
        
        if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
            phoneButton.alpha = 0.7;
            phoneButton.userInteractionEnabled = NO;
        }
    }
    
}

#pragma mark - User Interaction

- (IBAction)teacherWebsitePressed:(id)sender {
    // Push the web browser view controller
    WebBrowserViewController *webBrowser = [[WebBrowserViewController alloc] initWithNibName:@"WebBrowserViewController" urlString:_teacher.teacherUrl];
    webBrowser.title = self.title;
    [self.navigationController pushViewController:webBrowser animated:YES];
}

- (IBAction)emailPressed:(id)sender {
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:[NSArray arrayWithObject:_teacher.email]];
    [self presentModalViewController:controller animated:YES];

}

- (IBAction)phoneButtonPressed:(id)sender {
    NSString *message = [NSString stringWithFormat:NSLocalizedString(@"MESSAGE_CALL_NUMBER", nil), _teacher.phone];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ALERT_NO", nil) otherButtonTitles:NSLocalizedString(@"ALERT_YES", nil), nil];
    [alertView show];
}


#pragma mark - Mail Controller Delegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    // Dismiss Email Controller
    [controller dismissModalViewControllerAnimated:YES];
    NSString *message = nil;
    if (result == MFMailComposeResultSent) {
       message = NSLocalizedString(@"MAIL_SENT_SUCCESSFULLY", nil);
    } else if (result == MFMailComposeResultFailed) {
        message = NSLocalizedString(@"MAIL_SENT_ERROR", nil);
    }
    if (message) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"ALERT_OK", nil) otherButtonTitles: nil];
        [alert show];
    }
}

#pragma mark - UIAlertView delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        // Can call the phone number
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", _teacher.phone]]];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTeacherWebsiteButton:nil];
    [self setEmailButton:nil];
    [self setPhoneButton:nil];
    [super viewDidUnload];
}
@end
